package com.ttg.web.service.job.impl;

import com.google.common.collect.Lists;
import com.ttg.common.dto.TaskCostGradingDTO;
import com.ttg.common.dto.TaskCostGradingJobDTO;
import com.ttg.common.enums.IntervalTypeEnum;
import com.ttg.common.enums.SymbolEnum;
import com.ttg.common.exception.BaseException;
import com.ttg.common.utils.BigDecimalUtils;
import com.ttg.common.utils.DateTimeUtil;
import com.ttg.common.utils.IntegerUtil;
import com.ttg.common.vo.TaskResultVO;
import com.ttg.model.po.BankPublicMerchTradePrimitivePO;
import com.ttg.model.pojo.CostManageCostGradingRule;
import com.ttg.model.pojo.CostManageMerchTradeMonth;
import com.ttg.model.vo.MerchTradePrimitiveSumVO;
import com.ttg.web.dao.BankPublicMerchTradePrimitiveDao;
import com.ttg.web.dao.CostManageCostGradingRuleDao;
import com.ttg.web.dao.CostManageMerchTradeMonthDao;
import com.ttg.web.service.job.CostGradingJobService;
import com.ttg.web.service.job.JobCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Description：商户成本分档任务
 * Author: chenyou
 * Date: 2025/10/11 9:48
 * Company: ttg
 * Copyright: Copyright (c) 2025
 */
@Service
public class CostGradingJobServiceImpl extends JobCommon implements CostGradingJobService {

    @Autowired
    private CostManageMerchTradeMonthDao tradeMonthDao;
    @Autowired
    private CostManageCostGradingRuleDao costGradingRuleDao;
    @Autowired
    private BankPublicMerchTradePrimitiveDao tradePrimitiveDao;

    //保留小数位
    private static final int RATE_SCALE = 2;
    private static final int BATCH_SIZE = 500;

    @Override
    public TaskResultVO costGrading(TaskCostGradingDTO reqDto) {
        //检查日期参数
        reqDto = getCheckDate(reqDto);
        List<CostManageCostGradingRule> rulelist = costGradingRuleDao.getRulelist();
        List<TaskCostGradingJobDTO> jobDtoList = bulidReqDtoList(reqDto);
        for(TaskCostGradingJobDTO jobDTO:jobDtoList){
            execJob(jobDTO,rulelist);
        }

        return null;
    }

    public List<TaskCostGradingJobDTO> bulidReqDtoList(TaskCostGradingDTO reqDto){
        List<TaskCostGradingJobDTO> dtoList = new ArrayList<>();
        //组装按月执行的入参dto
        TaskCostGradingJobDTO dto1 = new TaskCostGradingJobDTO();
        dto1.setIntervalType(IntervalTypeEnum.月度区间.getValue());
        dto1.setDate(reqDto.getDate());
        dto1.setYear(reqDto.getYear());
        dto1.setMonth(reqDto.getMonth());
        dtoList.add(dto1);
        //按年累计从第二个月开始组装dto
        if(Integer.parseInt(reqDto.getMonth())>=2){
            TaskCostGradingJobDTO dto2 = new TaskCostGradingJobDTO();
            dto2.setIntervalType(IntervalTypeEnum.累计区间.getValue());
            dto2.setDate(reqDto.getDate());
            dto2.setYear(reqDto.getYear());
            dto2.setMonth(reqDto.getMonth());
            dto2.setLowerBound("01");
            dto2.setUpperBound(reqDto.getMonth());
            dtoList.add(dto2);
        }
        return dtoList;
    }

    public void execJob(TaskCostGradingJobDTO reqDto,List<CostManageCostGradingRule> rulelist){
        BankPublicMerchTradePrimitivePO po = new BankPublicMerchTradePrimitivePO();
        if(IntervalTypeEnum.月度区间.getValue().equals(reqDto.getIntervalType())) {
            po.setStartDate(DateTimeUtil.getFirstDayOnMonth(reqDto.getYear(),reqDto.getMonth()));
            po.setEndDate(DateTimeUtil.getLastDayOnMonth(reqDto.getYear(),reqDto.getMonth()));
        }else if(IntervalTypeEnum.累计区间.getValue().equals(reqDto.getIntervalType())){
            po.setStartDate(DateTimeUtil.getFirstDayOnYear(reqDto.getYear()));
            po.setEndDate(DateTimeUtil.getLastDayOnMonth(reqDto.getYear(),reqDto.getMonth()));
        }else{
            throw new BaseException("错误区间类型");
        }

        //todo 按月或累计 活跃商户判定条件：周期类活跃天数>N天
        //todo 疑问：假设商户A 5月才开户,按累计1-5月判断是否活跃商户，可能因为1-4月无数据导致判断为非活跃商户
        int livenessDayLimit = 2;

        //1.查询指定年月的商户交易数据，如（202508）
        //分批查询
        List<Long> mctNoList = tradePrimitiveDao.getMerchNos(po);
        List<List<Long>> parts = Lists.partition(mctNoList,BATCH_SIZE);
        for(List<Long> partMct:parts){
            po.setMctNos(partMct);
            List<MerchTradePrimitiveSumVO> list = tradePrimitiveDao.getParam(po);

            int daysBetween = DateTimeUtil.daysBetween(po.getStartDate().toString(),po.getEndDate().toString()) + 1;
            List<CostManageMerchTradeMonth> monthList = list.stream().map(o -> {
                CostManageMerchTradeMonth vo = new CostManageMerchTradeMonth();
                vo.setMtmMctNo(o.getMctNo());
                vo.setMtmAgentNo(o.getAgentNo());
                vo.setMtmOrgNo(o.getOrgNo());
                vo.setMtmIntervalType(reqDto.getIntervalType());
                vo.setMtmYear(reqDto.getYear());
                vo.setMtmMonth(reqDto.getMonth());
                vo.setMtmLowerBound(reqDto.getLowerBound());
                vo.setMtmUpperBound(reqDto.getUpperBound());
                // 周期活跃商户判定
                vo.setMtmLivenessFlag(IntegerUtil.ge(o.getLivenessDay(),livenessDayLimit)?1:0);
                // 日均存款
                BigDecimal dayAvgAmt = BigDecimalUtils.div(o.getTimeAmt(),daysBetween,4);
                // 计算清算成本 = 贴费总额/日均存款
                vo.setMtmClearCost(BigDecimalUtils.div(o.getRepairAmt(),dayAvgAmt));
                // 当前商户分档编号
                Integer gradingNo = costGrading(rulelist,vo);
                vo.setMtmGradingNo(gradingNo);
                vo.setMtmRepairCost(o.getRepairAmt());
                vo.setMtmDayAvgAmt(dayAvgAmt);
                vo.setMtmTradeAmt(o.getTradeAmt());
                vo.setMtmCreditTradeAmt(o.getCreditTradeAmt());
                return vo;
            }).collect(Collectors.toList());

            //入库保存
            tradeMonthDao.saveOrUpdateBatch(monthList);
        }

    }

//    public List<CostGradingPO> costGradingPOList(TaskCostGradingJobDTO reqDto, Map<Integer,List<BankPublicMerchTradePrimitiveVO>> gradingMap, Integer totalMctCount){
//        //按成本分档范围：统计商户数，贴费成本，日均存款，户数占比，万元补贴水平等......
//        List<CostGradingPO> costGradingPOList = new ArrayList<>();
//        for(Map.Entry<Integer,List<BankPublicMerchTradePrimitiveVO>> entry: gradingMap.entrySet()){
//            CostGradingPO costGradingPO = new CostGradingPO();
//
//            Integer gradingNo = entry.getKey();
//            List<BankPublicMerchTradePrimitiveVO> sublist = entry.getValue();
//
//            Integer mctCount = sublist.size();
//            BigDecimal tradeAmt = BigDecimal.ZERO;
//            BigDecimal repairCost = BigDecimal.ZERO;
//            BigDecimal dayAvgAmt = BigDecimal.ZERO;
//            BigDecimal creditCardAmt = BigDecimal.ZERO;
//            for(BankPublicMerchTradePrimitiveVO vo:sublist){
//                tradeAmt = BigDecimalUtils.add(tradeAmt,vo.getTradeAmt());
//                repairCost = BigDecimalUtils.add(repairCost,vo.getRepairAmt());
//                dayAvgAmt = BigDecimalUtils.add(dayAvgAmt,vo.getDayAvgAmt());
//                creditCardAmt = BigDecimalUtils.add(creditCardAmt,vo.getCreditTradeAmt());
//            }
//
//            BigDecimal mctCountRatio = BigDecimalUtils.div(mctCount,totalMctCount,4);
//            BigDecimal wanyuanRepairRatio = BigDecimalUtils.div(BigDecimalUtils.mul(repairCost,10000),tradeAmt,2);
//            BigDecimal mctDvgCost = BigDecimalUtils.div(repairCost,mctCount,2);
//            BigDecimal creditCardRatio = BigDecimalUtils.div(creditCardAmt,tradeAmt,4);
//
//            costGradingPO.setIntervalType(reqDto.getIntervalType());
//            costGradingPO.setYear(reqDto.getYear());
//            costGradingPO.setMonth(reqDto.getMonth());
//            costGradingPO.setMctDimType(reqDto.getMerchDimType());
//            costGradingPO.setSumFlag("single");
//            costGradingPO.setGradingNo(gradingNo);
//            //划断分批规则:1-6同批次,7-11同批次,12-13同批次,14-16同批次,17-20同批次
//            costGradingPO.setCutOffEnum(getCutOffEnum(gradingNo));
//            costGradingPO.setMctCount(mctCount);
//            costGradingPO.setTradeAmt(tradeAmt);
//            costGradingPO.setRepairCost(repairCost);
//            costGradingPO.setDayAvgDeposit(dayAvgAmt);
//            costGradingPO.setCreditCardAmt(creditCardAmt);
//            costGradingPO.setMctCountRatio(mctCountRatio);
//            costGradingPO.setWanYuanRepairRatio(wanyuanRepairRatio);
//            costGradingPO.setMctDvgCost(mctDvgCost);
//            costGradingPO.setCreditCardRatio(creditCardRatio);
//            costGradingPO.setAddTime(LocalDateTime.now());
//            costGradingPO.setUpdateTime(LocalDateTime.now());
//            costGradingPOList.add(costGradingPO);
//        }
//        return costGradingPOList;
//    }
//
//    public void sum1(List<CostGradingPO> costGradingPOList, MerchDimTypeEnum merchDimTypeEnum, String month){
//        //分档1-13小计
//        List<CostGradingPO> sublist_1_13 =
//                costGradingPOList.stream().filter(o -> o.getCutOffEnum().equals(GradingCutOffEnum.CUT_OFF_1_6)
//                        || o.getCutOffEnum().equals(GradingCutOffEnum.CUT_OFF_7_11)
//                        || o.getCutOffEnum().equals(GradingCutOffEnum.CUT_OFF_12_13)).collect(Collectors.toList());
//
//        Integer Bsum1 = 0;
//        BigDecimal Csum1 = BigDecimal.ZERO;
//        BigDecimal Dsum1 = BigDecimal.ZERO;
//        BigDecimal Esum1 = BigDecimal.ZERO;
//
//        for(CostGradingPO vo:sublist_1_13){
//            //Bsum1 = B1+B2+B3+ … +B13
//            Bsum1 = Bsum1 + vo.getCmcgMctCount();
//            //Csum1 = C1+C2+C3+ … +C13
//            Csum1 = BigDecimalUtils.add(Csum1,vo.getCmcgRepairCost());
//            //Dsum1 = D1+D2+D3+ … +D13
//            Dsum1 = BigDecimalUtils.add(Dsum1,vo.getCmcgDayAvgDeposit());
//            //Esum1 = E1+E2+E3+ … +E13
//            Esum1 = BigDecimalUtils.add(Esum1,vo.getCmcgMctCountRatio());
//        }
//        BigDecimal tradeAmt1 = new BigDecimal(sublist_1_13.stream().mapToLong(o -> o.getTradeAmt()==null?0:o.getTradeAmt().longValue()).sum());
//        BigDecimal creditCardAmt1 = new BigDecimal(sublist_1_13.stream().mapToLong(o -> o.getCreditCardAmt()==null?0:o.getCreditCardAmt().longValue()).sum());
//        //Fsum1 = Csum1/(1-13档全部商户总流水)
//        BigDecimal Fsum1 = BigDecimalUtils.div(Csum1,tradeAmt1,2);
//        //Gsum1 = Csum1/Bsum1
//        BigDecimal Gsum1 = BigDecimalUtils.div(Csum1,Bsum1,2);
//        //Hsum1 = 1-13档全部商户信用卡交易金额占比
//        BigDecimal Hsum1 =  BigDecimalUtils.div(creditCardAmt1,tradeAmt1,4);
//        //本地线程保存,方便后续使用
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Bsum1",new BigDecimal(Bsum1));
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Csum1",Csum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Dsum1",Dsum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Esum1",Esum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Fsum1",Fsum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Gsum1",Gsum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Hsum1",Hsum1);
//    }
//
//    public void sum2(List<CostGradingPO> costGradingPOList, MerchDimTypeEnum merchDimTypeEnum, String month){
//        List<CostGradingPO> sublist_14_20 =
//                costGradingPOList.stream().filter(o -> o.getCutOffEnum().equals(GradingCutOffEnum.CUT_OFF_14_16)
//                        || o.getCutOffEnum().equals(GradingCutOffEnum.CUT_OFF_17_20)).collect(Collectors.toList());
//        Integer Bsum2 = 0;
//        BigDecimal Csum2 = BigDecimal.ZERO;
//        BigDecimal Dsum2 = BigDecimal.ZERO;
//        BigDecimal Esum2 = BigDecimal.ZERO;
//
//        for(CostGradingPO vo:sublist_14_20){
//            //Bsum2 = B14+B15+ … +B20
//            Bsum2 = Bsum2 + vo.getCmcgMctCount();
//            //Csum2 = C14+C15+ … +C20
//            Csum2 = BigDecimalUtils.add(Csum2,vo.getCmcgRepairCost());
//            //Dsum2 = D14+D15+ … +D20
//            Dsum2 = BigDecimalUtils.add(Dsum2,vo.getCmcgDayAvgDeposit());
//            //Esum2 = E14+E15+ … +E20
//            Esum2 = BigDecimalUtils.add(Esum2,vo.getCmcgMctCountRatio());
//        }
//        BigDecimal tradeAmt2 = new BigDecimal(sublist_14_20.stream().mapToLong(o -> o.getTradeAmt()==null?0:o.getTradeAmt().longValue()).sum());
//        BigDecimal creditCardAmt2 = new BigDecimal(sublist_14_20.stream().mapToLong(o -> o.getCreditCardAmt()==null?0:o.getCreditCardAmt().longValue()).sum());
//        //Fsum2 = Csum2/(14-20档全部商户总流水)
//        BigDecimal Fsum2 = BigDecimalUtils.div(Csum2,tradeAmt2,2);
//        //Gsum2 = Csum2/Bsum2
//        BigDecimal Gsum2 = BigDecimalUtils.div(Csum2,Bsum2,2);
//        //Hsum2 = 14-20档全部商户信用卡交易金额占比
//        BigDecimal Hsum2 =  BigDecimalUtils.div(creditCardAmt2,tradeAmt2,4);
//        //本地线程保存,方便后续使用
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Bsum2",new BigDecimal(Bsum2));
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Csum2",Csum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Dsum2",Dsum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Esum2",Esum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Fsum2",Fsum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Gsum2",Gsum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Hsum2",Hsum2);
//    }
//
//    private void sum3(List<CostGradingPO> costGradingPOList, MerchDimTypeEnum merchDimTypeEnum, String month){
//        BigDecimal tradeAmt3 = new BigDecimal(costGradingPOList.stream().mapToLong(o -> o.getTradeAmt()==null?0:o.getTradeAmt().longValue()).sum());
//        BigDecimal creditCardAmt3 = new BigDecimal(costGradingPOList.stream().mapToLong(o -> o.getCreditCardAmt()==null?0:o.getCreditCardAmt().longValue()).sum());
//        //Bsum3 = Bsum1+Bsum2
//        Integer Bsum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum2")).intValue();
//        //Csum3 = Csum1+Csum2
//        BigDecimal Csum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum2"));
//        //Dsum3 = Dsum1+Dsum2
//        BigDecimal Dsum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum2"));
//        //Esum3 = Esum1+Esum2
//        BigDecimal Esum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Esum1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Esum2"));
//        //Fsum3 = Csum3/(1-20档全部商户总流水)
//        BigDecimal Fsum3 = BigDecimalUtils.div(Csum3,tradeAmt3,2);
//        //Gsum3 = Csum2/Bsum2
//        BigDecimal Gsum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum2"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum2"));
//        //Hsum3 = 1-20档全部商户信用卡交易金额占比
//        BigDecimal Hsum3 = BigDecimalUtils.div(creditCardAmt3,tradeAmt3,4);
//        //本地线程保存,方便后续使用
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Bsum3",new BigDecimal(Bsum3));
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Csum3",Csum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Dsum3",Dsum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Esum3",Esum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Fsum3",Fsum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Gsum3",Gsum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Hsum3",Hsum3);
//    }
//
//    public List<CostGradingCutoffBatchPO> cutoffBatchList(TaskCostGradingJobDTO reqDto, List<CostGradingPO> costGradingPOList, MerchDimTypeEnum merchDimTypeEnum, String month){
//        List<CostGradingCutoffBatchPO> cutoffBatchList = new ArrayList<>();
//        Map<GradingCutOffEnum,List<CostGradingPO>> cutOffEnumListMap = costGradingPOList.stream().collect(Collectors.groupingBy(o -> o.getCutOffEnum()));
//        for(Map.Entry<GradingCutOffEnum,List<CostGradingPO>> entry:cutOffEnumListMap.entrySet()){
//            CostGradingCutoffBatchPO cutoffBatchPO = new CostGradingCutoffBatchPO();
//            GradingCutOffEnum cutOffEnum = entry.getKey();
//            List<CostGradingPO> subList = entry.getValue();
//            //划断贴费成本=分档内贴费成本汇总
//            BigDecimal repairCost = new BigDecimal(subList.stream().mapToLong(o -> o.getCmcgRepairCost()==null?0:o.getCmcgRepairCost().longValue()).sum());
//            //贴费成本占比=划断贴费成本/全体商户贴费成本汇总(Csum3)
//            BigDecimal repairCostRatio = BigDecimalUtils.div(repairCost,CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum3"),4);
//            //日均存款 = 分档内日均存款汇总
//            BigDecimal dayAvgDeposit = new BigDecimal(subList.stream().mapToLong(o -> o.getCmcgDayAvgDeposit()==null?0:o.getCmcgDayAvgDeposit().longValue()).sum());
//            //日均存款占比 = 日均存款/全体商户日均存款汇总(Dsum3)
//            BigDecimal dayAvgDepositRatio = BigDecimalUtils.div(dayAvgDeposit,CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum3"),4);
//            //户数 = 分档内商户数汇总
//            Integer mctCount = subList.stream().mapToInt(o -> o.getCmcgMctCount()==null?0:o.getCmcgMctCount().intValue()).sum();
//            //户数占比 = 户数/全体商户数(Bsum3)
//            BigDecimal mctCountRatio = BigDecimalUtils.div(mctCount,CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum3"),4);
//            //生成批次号
//            String batchNo = generateCostGradingBatchNo(reqDto);
//            cutoffBatchPO.setCutOffEnum(cutOffEnum);
//            cutoffBatchPO.setCgcbBatchNo(batchNo);
//            cutoffBatchPO.setCgcbIntervalType(reqDto.getIntervalType());
//            cutoffBatchPO.setCgcbYear(reqDto.getYear());
//            cutoffBatchPO.setCgcbMonth(reqDto.getMonth());
//            cutoffBatchPO.setCgcbMctDimType(reqDto.getMerchDimType());
//            cutoffBatchPO.setCgcbSumFlag("single");
//            cutoffBatchPO.setCgcbCutoff(cutOffEnum.getValue());
//            cutoffBatchPO.setCgcbRepairCost(repairCost);
//            cutoffBatchPO.setCgcbRepairCostRatio(repairCostRatio);
//            cutoffBatchPO.setCgcbDayAvgDeposit(dayAvgDeposit);
//            cutoffBatchPO.setCgcbDayAvgDepositRatio(dayAvgDepositRatio);
//            cutoffBatchPO.setCgcbMctCount(mctCount);
//            cutoffBatchPO.setCgcbMctCountRatio(mctCountRatio);
//            cutoffBatchList.add(cutoffBatchPO);
//
//            //本地线程保存,方便后续使用
//            if(GradingCutOffEnum.CUT_OFF_1_6.equals(cutOffEnum)){
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum4",repairCost);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum4",repairCostRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum4",dayAvgDeposit);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum4",dayAvgDepositRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"M4",new BigDecimal(mctCount));
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"N4",mctCountRatio);
//            }else if(GradingCutOffEnum.CUT_OFF_7_11.equals(cutOffEnum)){
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum5",repairCost);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum5",repairCostRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum5",dayAvgDeposit);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum5",dayAvgDepositRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"M5",new BigDecimal(mctCount));
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"N5",mctCountRatio);
//            }else if(GradingCutOffEnum.CUT_OFF_12_13.equals(cutOffEnum)){
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum6",repairCost);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum6",repairCostRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum6",dayAvgDeposit);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum6",dayAvgDepositRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"M6",new BigDecimal(mctCount));
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"N6",mctCountRatio);
//            }else if(GradingCutOffEnum.CUT_OFF_14_16.equals(cutOffEnum)){
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum7",repairCost);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum7",repairCostRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum7",dayAvgDeposit);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum7",dayAvgDepositRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"M7",new BigDecimal(mctCount));
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"N7",mctCountRatio);
//            }else if(GradingCutOffEnum.CUT_OFF_17_20.equals(cutOffEnum)){
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum8",repairCost);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum8",repairCostRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum8",dayAvgDeposit);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum8",dayAvgDepositRatio);
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"M8",new BigDecimal(mctCount));
//                CostGradingUtil.putResult(merchDimTypeEnum,month,"N8",mctCountRatio);
//            }
//        }
//        return cutoffBatchList;
//    }
//
//    public void batchSum1(MerchDimTypeEnum merchDimTypeEnum,String month){
//        //贴费成本 Isum1 = Isum4+Isum5+Isum6
//        BigDecimal Isum1 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum4"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum5"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum6"));
//        //贴费成本占比 Jsum1 = 贴费成本/全体商户贴费成本汇总(Csum3)
//        BigDecimal Jsum1 = BigDecimalUtils.div(Isum1,CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum3"));
//        //日均存款 Ksum1 = Ksum4+Ksum5+Ksum6
//        BigDecimal Ksum1 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum4"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum5"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum6"));
//        //日均存款占比 Lsum1 = 日均存款/全体商户日均存款汇总(Dsum3)
//        BigDecimal Lsum1 = BigDecimalUtils.div(Ksum1,CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum3"),4);
//        //户数 M1 = M4+M5+M6
//        BigDecimal M1 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"M4"),CostGradingUtil.getResult(merchDimTypeEnum,month,"M5"),CostGradingUtil.getResult(merchDimTypeEnum,month,"M6"));
//        //户数占比 N1 = 户数/全体商户数(Bsum3)
//        BigDecimal N1 = BigDecimalUtils.div(M1,CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum3"),4);
//        //本地线程保存,方便后续使用
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum1",Isum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum1",Jsum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum1",Ksum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum1",Lsum1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"M1",M1);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"N1",N1);
//    }
//
//    public void batchSum2(MerchDimTypeEnum merchDimTypeEnum,String month){
//        //贴费成本 Isum2 = Isum7+Isum8
//        BigDecimal Isum2 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum7"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum8"));
//        //贴费成本占比 Jsum2 = 贴费成本/全体商户贴费成本汇总(Csum3)
//        BigDecimal Jsum2 = BigDecimalUtils.div(Isum2,CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum3"));
//        //日均存款 Ksum2 = Ksum7+Ksum8
//        BigDecimal Ksum2 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum7"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum8"));
//        //日均存款占比 Lsum2 = 日均存款/全体商户日均存款汇总(Dsum3)
//        BigDecimal Lsum2 = BigDecimalUtils.div(Ksum2,CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum3"),4);
//        //户数 M2 = M7+M8
//        BigDecimal M2 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"M7"),CostGradingUtil.getResult(merchDimTypeEnum,month,"M8"));
//        //户数占比 N2 = 户数/全体商户数(Bsum3)
//        BigDecimal N2 = BigDecimalUtils.div(M2,CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum3"),4);
//        //本地线程保存,方便后续使用
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum2",Isum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum2",Jsum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum2",Ksum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum2",Lsum2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"M2",M2);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"N2",N2);
//    }
//
//    public void batchSum3(MerchDimTypeEnum merchDimTypeEnum,String month){
//        //贴费成本 Isum3 = Isum1+Isum2
//        BigDecimal Isum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum2"));
//        //贴费成本占比 Jsum3 = 贴费成本/全体商户贴费成本汇总(Csum3)
//        BigDecimal Jsum3 = BigDecimalUtils.div(Isum3,CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum3"));
//        //日均存款 Ksum3 = Ksum1+Ksum2
//        BigDecimal Ksum3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum2"));
//        //日均存款占比 Lsum3 = 日均存款/全体商户日均存款汇总(Dsum3)
//        BigDecimal Lsum3 = BigDecimalUtils.div(Ksum3,CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum3"),4);
//        //户数 M3 = M1+M2
//        BigDecimal M3 = BigDecimalUtils.add(CostGradingUtil.getResult(merchDimTypeEnum,month,"M1"),CostGradingUtil.getResult(merchDimTypeEnum,month,"M2"));
//        //户数占比 N3 = 户数/全体商户数(Bsum3)
//        BigDecimal N3 = BigDecimalUtils.div(M3,CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum3"),4);
//        //本地线程保存,方便后续使用
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Isum3",Isum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Jsum3",Jsum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Ksum3",Ksum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"Lsum3",Lsum3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"M3",M3);
//        CostGradingUtil.putResult(merchDimTypeEnum,month,"N3",N3);
//    }
//
//    public void bulidCostGradingSumInfo(TaskCostGradingJobDTO reqDto, List<CostGradingPO> costGradingPOList, MerchDimTypeEnum merchDimTypeEnum, String month){
//        //小计1
//        CostGradingPO sum1 = new CostGradingPO();
//        sum1.setCmcgIntervalType(reqDto.getIntervalType());
//        sum1.setCmcgYear(reqDto.getYear());
//        sum1.setCmcgMonth(reqDto.getMonth());
//        sum1.setCmcgMctDimType(reqDto.getMerchDimType());
//        sum1.setCmcgSumFlag("sum1");
//        sum1.setCmcgMctCount(CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum1").intValue());
//        sum1.setCmcgRepairCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum1"));
//        sum1.setCmcgDayAvgDeposit(CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum1"));
//        sum1.setCmcgMctCountRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Esum1"));
//        sum1.setCmcgWanYuanRepairRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Fsum1"));
//        sum1.setCmcgMctDvgCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Gsum1"));
//        sum1.setCmcgCreditCardRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Hsum1"));
//
//        //小计2
//        CostGradingPO sum2 = new CostGradingPO();
//        sum2.setCmcgIntervalType(reqDto.getIntervalType());
//        sum2.setCmcgYear(reqDto.getYear());
//        sum2.setCmcgMonth(reqDto.getMonth());
//        sum2.setCmcgMctDimType(reqDto.getMerchDimType());
//        sum2.setCmcgSumFlag("sum2");
//        sum2.setCmcgMctCount(CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum2").intValue());
//        sum2.setCmcgRepairCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum2"));
//        sum2.setCmcgDayAvgDeposit(CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum2"));
//        sum2.setCmcgMctCountRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Esum2"));
//        sum2.setCmcgWanYuanRepairRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Fsum2"));
//        sum2.setCmcgMctDvgCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Gsum2"));
//        sum2.setCmcgCreditCardRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Hsum2"));
//
//        //合计
//        CostGradingPO sum3 = new CostGradingPO();
//        sum3.setCmcgIntervalType(reqDto.getIntervalType());
//        sum3.setCmcgYear(reqDto.getYear());
//        sum3.setCmcgMonth(reqDto.getMonth());
//        sum3.setCmcgMctDimType(reqDto.getMerchDimType());
//        sum3.setCmcgSumFlag("sum3");
//        sum3.setCmcgMctCount(CostGradingUtil.getResult(merchDimTypeEnum,month,"Bsum3").intValue());
//        sum3.setCmcgRepairCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Csum3"));
//        sum3.setCmcgDayAvgDeposit(CostGradingUtil.getResult(merchDimTypeEnum,month,"Dsum3"));
//        sum3.setCmcgMctCountRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Esum3"));
//        sum3.setCmcgWanYuanRepairRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Fsum3"));
//        sum3.setCmcgMctDvgCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Gsum3"));
//        sum3.setCmcgCreditCardRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Hsum3"));
//
//        costGradingPOList.add(sum1);
//        costGradingPOList.add(sum2);
//        costGradingPOList.add(sum3);
//    }
//
//    public void bulidCutoffBatchSumInfo(TaskCostGradingJobDTO reqDto, List<CostGradingCutoffBatchPO> cutoffBatchList, MerchDimTypeEnum merchDimTypeEnum, String month){
//        //小计1
//        CostGradingCutoffBatchPO batchSum1 = new CostGradingCutoffBatchPO();
//        batchSum1.setCgcbBatchNo(generateCostGradingBatchNo(reqDto));
//        batchSum1.setCgcbIntervalType(reqDto.getIntervalType());
//        batchSum1.setCgcbYear(reqDto.getYear());
//        batchSum1.setCgcbMonth(reqDto.getMonth());
//        batchSum1.setCgcbMctDimType(reqDto.getMerchDimType());
//        batchSum1.setCgcbSumFlag("sum1");
//        batchSum1.setCgcbRepairCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum1"));
//        batchSum1.setCgcbRepairCostRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Jsum1"));
//        batchSum1.setCgcbDayAvgDeposit(CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum1"));
//        batchSum1.setCgcbDayAvgDepositRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Lsum1"));
//        batchSum1.setCgcbMctCount(CostGradingUtil.getResult(merchDimTypeEnum,month,"M1").intValue());
//        batchSum1.setCgcbMctCountRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"N1"));
//        cutoffBatchList.add(batchSum1);
//        //小计2
//        CostGradingCutoffBatchPO batchSum2 = new CostGradingCutoffBatchPO();
//        batchSum2.setCgcbBatchNo(generateCostGradingBatchNo(reqDto));
//        batchSum2.setCgcbIntervalType(reqDto.getIntervalType());
//        batchSum2.setCgcbYear(reqDto.getYear());
//        batchSum2.setCgcbMonth(reqDto.getMonth());
//        batchSum2.setCgcbMctDimType(reqDto.getMerchDimType());
//        batchSum2.setCgcbSumFlag("sum2");
//        batchSum2.setCgcbRepairCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum2"));
//        batchSum2.setCgcbRepairCostRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Jsum2"));
//        batchSum2.setCgcbDayAvgDeposit(CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum2"));
//        batchSum2.setCgcbDayAvgDepositRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Lsum2"));
//        batchSum2.setCgcbMctCount(CostGradingUtil.getResult(merchDimTypeEnum,month,"M2").intValue());
//        batchSum2.setCgcbMctCountRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"N2"));
//        cutoffBatchList.add(batchSum2);
//        //合计
//        CostGradingCutoffBatchPO batchSum3 = new CostGradingCutoffBatchPO();
//        batchSum3.setCgcbBatchNo(generateCostGradingBatchNo(reqDto));
//        batchSum3.setCgcbIntervalType(reqDto.getIntervalType());
//        batchSum3.setCgcbYear(reqDto.getYear());
//        batchSum3.setCgcbMonth(reqDto.getMonth());
//        batchSum3.setCgcbMctDimType(reqDto.getMerchDimType());
//        batchSum3.setCgcbSumFlag("sum3");
//        batchSum3.setCgcbRepairCost(CostGradingUtil.getResult(merchDimTypeEnum,month,"Isum3"));
//        batchSum3.setCgcbRepairCostRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Jsum3"));
//        batchSum3.setCgcbDayAvgDeposit(CostGradingUtil.getResult(merchDimTypeEnum,month,"Ksum3"));
//        batchSum3.setCgcbDayAvgDepositRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"Lsum3"));
//        batchSum3.setCgcbMctCount(CostGradingUtil.getResult(merchDimTypeEnum,month,"M3").intValue());
//        batchSum3.setCgcbMctCountRatio(CostGradingUtil.getResult(merchDimTypeEnum,month,"N3"));
//        cutoffBatchList.add(batchSum3);
//    }

    /**
     * 分档计算
     * @param rulelist
     * @param tradeMonth
     * @return Integer
     * @date: 2025/10/13 17:22
     * @author: chenyou
     */
    public Integer costGrading(List<CostManageCostGradingRule> rulelist,CostManageMerchTradeMonth tradeMonth){
        //分档计算
        Integer gradingNo = 0;
        for(CostManageCostGradingRule rule : rulelist){
            boolean result = gradingCost(tradeMonth.getMtmClearCost(),rule.getCgrValue(),rule.getCgrSymbol());
            if(result){
                gradingNo = rule.getCgrNo();
                break;
            }
        }
        return gradingNo;
    }

    /**
     * 根据清算成本匹配分档
     * @param clearCost 清算成本
     * @param gradingCost 分档成本
     * @param symbol 条件符号 包括(>,>=,==,<=,<,!=)
     * @return: boolean
     * @date: 2025/9/2 15:14
     * @author: chenyou
     */
    public static boolean gradingCost(BigDecimal clearCost, BigDecimal gradingCost, String symbol){
        if(SymbolEnum.getByVal(symbol)==null){
            throw new BaseException("错误的比较符号");
        }
        boolean result = false;
        switch (SymbolEnum.getByVal(symbol)){
            case 大于:
                result = clearCost.compareTo(gradingCost)>0;
                break;
            case 大于等于:
                result = clearCost.compareTo(gradingCost)>=0;
                break;
            case 等于:
                result = clearCost.compareTo(gradingCost)==0;
                break;
            case 小于等于:
                result = clearCost.compareTo(gradingCost)<=0;
                break;
            case 小于:
                result = clearCost.compareTo(gradingCost)<0;
                break;
            case 不等于:
                result = clearCost.compareTo(gradingCost)!=0;
                break;
        }
        return result;
    }

//    /**
//     * 划断分批规则:1-6同批次,7-11同批次,12-13同批次,14-16同批次,17-20同批次
//     * @params: gradingNo 分档编号
//     * @return: com.ttg.common.enums.GradingCutOffEnum
//     * @date: 2025/10/14 9:56
//     * @author: chenyou
//     */
//    public GradingCutOffEnum getCutOffEnum(Integer gradingNo){
//        return GradingCutOffEnum.getByCode(gradingNo);
//    }
//
//    public static String generateCostGradingBatchNo(TaskCostGradingJobDTO reqDto){
//        StringBuffer batchNo = new StringBuffer(reqDto.getYear());
//        batchNo.append(reqDto.getMonth());
//        long millis = System.currentTimeMillis();
//        //11位随机数
//        String random = RandomStringUtils.randomNumeric(BasicConstant.INT_11);
//        batchNo.append("_" + millis).append(random);
//        return batchNo.toString();
//    }
}
